/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ex1;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import ex1.BundleTool;
import java.util.Locale;
import org.apache.struts.util.MessageResources;
/**
 *
 * @author O.Riad
 * @author D.Romain
 */
public class ProfileForm extends ActionForm {

    private String firstName;
    private String lastName;
    private String favoriteColor;
    private String birthdate;
    private String address;
    private String phone;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getFavoriteColor() {
        return favoriteColor;
    }

    public void setFavoriteColor(String favoriteColor) {
        this.favoriteColor = favoriteColor;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.address = null;
        this.birthdate = null;
        this.favoriteColor = null;
        this.firstName = null;
        this.lastName = null;
        this.phone = null;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        
        BundleTool.setBundle(request.getLocale());

        ActionErrors errors = new ActionErrors();
        if ((this.firstName == null) || (this.firstName.length() < 1)) {
            ActionMessage error = new ActionMessage("error.firstName.required");
            errors.add("firstName", error);
        }
        if ((this.lastName == null) || (this.lastName.length() < 1)) {
            ActionMessage error = new ActionMessage("error.lastName.required");
            errors.add("lastName", error);
        }
        if ((this.favoriteColor == null) || (this.favoriteColor.length() < 1)) {
            ActionMessage error = new ActionMessage("error.favoriteColor.required");
            errors.add("favoriteColor", error);
        } else if (!BundleTool.getBundle().getString("app.valid.color").equals(this.favoriteColor)) {
            ActionMessage error =
                    new ActionMessage("error.favoriteColor.mustBeBlue");
            errors.add("favoriteColor", error);
        }
        if ((this.birthdate == null) || (this.birthdate.length() < 1)) {
            ActionMessage error = new ActionMessage("error.birthdate.required");
            errors.add("birthdate", error);
        } else if (!this.birthdate.matches("\\d+{1,2}/\\d{1,2}/\\d{2}")) {
            ActionMessage error = new ActionMessage("error.birthdate.invalid");
            errors.add("birthdate", error);
        }
        if (this.address == null || this.address.length() <1){
            ActionMessage error = new ActionMessage("error.address.required");
            errors.add("address", error);
        }
        if (this.phone == null || this.phone.length() <1){
            ActionMessage error = new ActionMessage("error.phone.required");
            errors.add("phone", error);
        }
        
        return errors;
    }
}
